<template>
	<view class="col363636 f26r pl20r pr20r">
		<view class="disFlex aliItCenter h100r borB1SE8e8e8">
			<view class="w160r teAliRight borR1SE8e8e8">
				绑定房间：
			</view>
			<view class="flex1 pl20r">
				<picker @change="picChaFun1" :value="picVal1" :range="picRang1" mode="selector"
					range-key="name" :disabled="pic1Disa"
					:class="['pl20r', 'pr20r', 'bor1SE8e8e8', 'lh70r', 'borRad10r',
						pic1Disa ? 'backColE7e7e7' : ''
					]"
				>
					<view class="disFlex">
						<view class="flex1">
							{{picRang1 && picRang1.length ? picRang1[picVal1].name : ''}}
						</view>
						<view class="w40r">
							<image src="/static/icons/iconDownGray.png" mode="aspectFit"
								class="w40r h40r poRel top8r"
							></image>
						</view>
					</view>
				</picker>
			</view>
		</view>
		<view class="disFlex aliItCenter h100r borB1SE8e8e8">
			<view class="w160r teAliRight borR1SE8e8e8">
				套餐名称：
			</view>
			<view class="flex1 pl20r">
				<input type="text" placeholder="请输入套餐名称"
					class="f26r h60r" v-model="newObj.name"
				>
			</view>
		</view>
		<view class="disFlex aliItCenter h100r borB1SE8e8e8">
			<view class="w160r teAliRight borR1SE8e8e8">
				套餐内容：
			</view>
			<view class="flex1 pl20r">
				<input type="text" placeholder="名称,数量,原价,现价|名称,数量,原价,现价"
					class="f26r h60r" v-model="newObj.content"
				>
			</view>
		</view>
		<!-- <view class="disFlex aliItCenter h100r borB1SE8e8e8">
			<view class="w160r teAliRight borR1SE8e8e8">
				套餐介绍：
			</view>
			<view class="flex1 pl20r">
				<input type="text" placeholder="请输入套餐介绍"
					class="f26r h60r" v-model="newObj.introduce"
				>
			</view>
		</view> -->
		<view class="disFlex aliItCenter h100r borB1SE8e8e8">
			<view class="w160r teAliRight borR1SE8e8e8">
				套餐价格：
			</view>
			<view class="flex1 pl20r">
				<input type="digit" placeholder="请输入套餐价格"
					class="f26r h60r" v-model="newObj.price"
				>
			</view>
		</view>
		<view class="disFlex aliItCenter h100r borB1SE8e8e8">
			<view class="w160r teAliRight borR1SE8e8e8">
				启用/禁用：
			</view>
			<view class="flex1 pl20r">
				<radio-group @change="radioChange1">
					<label v-for="(item, index) in radioAry1" :key="item.value"
						class="wPer50 disInBlo teAliCenter"
					>
						<radio :value="item.value"
							:checked="item.value === newObj.type"
							color="#64aeb4" style="transform: scale(0.8)"
						 />
						{{item.radioText}}
					</label>
				</radio-group>
			</view>
		</view>
		<button class="colFff lh70r mt40r f26r backCol64aeb4"
			@click="cliSubBtn"
		>
			提交
		</button>
	</view>
</template>
<script>
import { inquiryToLogin } from '@/utils/utils.js'
import { getRoomList, addTeaSetmeal, saveTeaSetmeal } from '@/utils/api.js'
export default {
	data() {
		return {
			optionType: '',
			optionData: {},
			radioAry1: [
				{value: '1', radioText: '启用'},
				{value: '2', radioText: '禁用'}
			],
			newObj: {
				token: '',
				rid: '',
				name: '',
				content: '',
				introduce: '无',
				price: '',
				type: '1'
			},
			picVal1: 0, picRang1: [], pic1Disa: false
		}
	},
	onLoad(options) {
		this.optionType = options.type;
		if(options.type == 1) {
			uni.setNavigationBarTitle({ title: '新增套餐' });
		}else if(options.type == 2) {
			uni.setNavigationBarTitle({ title: '修改套餐' });
			console.log(11, JSON.parse(options.data))
			this.optionData = JSON.parse(options.data);
			this.pic1Disa = true;
			this.newObj.name = this.optionData.name;
			this.newObj.content = this.optionData.content;
			this.newObj.introduce = this.optionData.introduce;
			this.newObj.price = this.optionData.price;
			this.newObj.type = this.optionData.type;
		}
	},
	onShow() {
		inquiryToLogin().then(res => {
			this.newObj.token = res;
			this.getDataList1();
		});
	},
	methods: {
		getDataList1() {
			getRoomList({token: this.newObj.token}).then(res1 => {
				if(res1.data && res1.data.length) {
					this.picRang1 = res1.data;
					if(this.optionType == 1) {
						this.newObj.rid = res1.data[this.picVal1].id
					}else {
						res1.data.forEach((item, index) => {
							if(item.id == this.optionData.rid) {
								this.newObj.rid = item.id;
								this.picVal1 = index;
							}
						})
					}
				}
			})
		},
		picChaFun1(eee) {
			this.picVal1 = eee.detail.value
			this.newObj.rid = this.picRang1[this.picVal1].id
		},
		radioChange1(evt) {
			for (let i = 0; i < this.radioAry1.length; i++) {
				if (this.radioAry1[i].value === evt.detail.value) {
					this.newObj.type = this.radioAry1[i].value;
					break;
				}
			}
		},
		cliSubBtn() {
			if(!this.newObj.rid) return uni.showToast({ title: '请输入绑定房间', icon: 'none' })
			if(!this.newObj.name) return uni.showToast({ title: '请输入套餐名称', icon: 'none' })
			if(!this.newObj.content) return uni.showToast({ title: '请输入套餐内容', icon: 'none' })
			// if(!this.newObj.introduce) return uni.showToast({ title: '请输入套餐介绍', icon: 'none' })
			if(!this.newObj.price) return uni.showToast({ title: '请输入套餐价格', icon: 'none' })
			if(this.optionType == 1) {
				addTeaSetmeal(this.newObj).then(res1 => {
					uni.showModal({
						title: '提示', content: '新增套餐成功', confirmColor: '#99c043',
						showCancel: false, confirmText: '返回套餐列表',
						success:res2 => {
							if (res2.confirm) {
								uni.navigateBack({ delta: 1 });
							}
						}
					})
				})
			}else if(this.optionType == 2) {
				if(this.newObj.name == this.optionData.name) {
					if(this.newObj.content == this.optionData.content) {
						// if(this.newObj.introduce == this.optionData.introduce) {
							if(this.newObj.price == this.optionData.price) {
								if(this.newObj.type == this.optionData.type) {
									return uni.showToast({
										title: '您没有改动套餐', icon: 'none', duration: 3000
									})
								}
							}
						// }
					}
				}
				saveTeaSetmeal({
					token: this.newObj.token,
					id: this.optionData.id,
					rid: this.newObj.rid,
					name: this.newObj.name,
					content: this.newObj.content,
					introduce: this.newObj.introduce,
					price: this.newObj.price,
					type: this.newObj.type
				}).then(res1 => {
					uni.showModal({
						title: '提示', content: '修改套餐成功', confirmColor: '#99c043',
						showCancel: false, confirmText: '返回套餐列表',
						success:res2 => {
							if (res2.confirm) {
								uni.navigateBack({ delta: 1 });
							}
						}
					})
				})
			}
		}
	}
}
</script>
<style>
</style>